$(function () {
    init_table();
});

function init_table() {
    $.ajax({
        url: '/get-waiting-orders',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.status) {
                var result_list = new Array(data.result);
                result_list.forEach((value, _idx, _arr) => {
                    value.forEach((sub_value, _sub_idx, _sub_arr) => {
                        $s_body = `
                            <tr>
                                <td>${sub_value['id']}</td>
                                <td>${sub_value['location']}</td>
                                <td>${sub_value['phone']}</td>
                                <td>${sub_value['money']}</td>
                                <td>
                                    <button class="btn btn-primary btn-sm export-button" uid="${sub_value['id']}">发货</button>
                                    <button class="btn btn-success btn-sm select-button" uid="${sub_value['id']}">查看</button>
                                </td>
                            </tr>
                        `;
                        $('#waiting-table-body').append($s_body);
                    });
                });
                export_shop();
            } else {
                alert('数据获取失败');
            }
        }
    });
    $.ajax({
        url: '/get-history-orders',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.status) {
                var result_list = new Array(data.result);
                result_list.forEach((value, _idx, _arr) => {
                    value.forEach((sub_value, _sub_idx, _sub_arr) => {
                        $s_body = `
                            <tr>
                                <td>${sub_value['id']}</td>
                                <td>${sub_value['location']}</td>
                                <td>${sub_value['phone']}</td>
                                <td>${sub_value['money']}</td>
                                <td>
                                    <button class="btn btn-success btn-sm select-button" uid="${sub_value['id']}">查看</button>
                                </td>
                            </tr>
                        `;
                        $('#history-table-body').append($s_body);
                    });
                });
                select_order();
            } else {
                alert('数据获取失败');
            }
        }
    });
    select_order();
}

function select_order() {
    var target_id = undefined;
    $('.select-button').click(function () {
        // console.log('hello');
        target_id = $(this).attr('uid');
        $.ajax({
            url: `/get-order-products?id=${target_id}`,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                // console.log(data);
                if (data.status) {
                    var result_list = new Array(data.result);
                    result_list.forEach((value, _idx, _arr) => {
                        // console.log(value);
                        value.forEach((sub_value, _sub_idx, _sub_arr) => {
                            $s_body = `
                                <tr>
                                    <td>${sub_value['order_id']}</td>
                                    <td>${sub_value['name']}</td>
                                    <td>${sub_value['product_id']}</td>
                                    <td>${sub_value['price']}</td>
                                    <td>${sub_value['num']}</td>
                                </tr>
                            `;
                            $('#order-info-table-body').append($s_body);
                        });
                    });
                } else {
                    alert('获取数据失败');
                }
            }
        });
        $('#select-Modal').modal('show');
    });
}

function export_shop() {
    var target_id = undefined;
    $('.export-button').click(function () {
        target_id = $(this).attr('uid');
        console.log(target_id);
        $.ajax({
            url: `/get-order-info?id=${target_id}`,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                if (data.status) {
                    var result = new Array(data.result);
                    $('#order-id').val(result[0]['id']);
                    $('#order-phone').val(result[0]['phone']);
                    $('#order-location').val(result[0]['location']);
                    $('#order-money').val(result[0]['money']);
                } else {
                    alert('数据获取失败');
                }
            },
        });
        $.ajax({
            url: `/logistic-list`,
            type: 'get',
            dataType: 'json',
            success: function (data) {
                if (data.status) {
                    var result = new Array(data.result);
                    result.forEach((val, _idx, _arr) => {
                        val.forEach((sub_val, _sub_idx, _sub_arr) => {
                            $s_body = `
                                <option value=${sub_val['id']}>${sub_val['name']}</option>
                            `;
                            $('#order-logistic').append($s_body);
                        });
                    });
                } else {
                    alert('数据获取失败');
                }
            }
        });
        $('#export-Modal').modal('show');
    });
    $('#export-confirm').click(function () {
        $.ajax({
            url: `/order-export?id=${target_id}`,
            type: 'post',
            dataType: 'json',
            data: {
                logistic: $('#order-logistic').val(),
                money: $('#order-money').val(),
            },
            success: function (data) {
                if (data.status) {
                    location.reload();
                } else {
                    console.log(data.status_code);
                    if (data.status_code == 0) {
                        alert('数据连接失败');
                    } else {
                        $('#export-confirm').attr('class', 'btn btn-danger');
                        $('#export-confirm').text('零件不足，请联系采购部采购');
                    }
                }
            }
        });
    });
}